home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 11 / develop 11 code / The NetWork Project / Examples (Sources) / NetSim / TaskCtl.p < prev    next >
Encoding:
Text File  |  1992-07-15  |  10.4 KB  |  382 lines  |  [TEXT/MPS ]

  1. {TaskCtl.p 
  2. 2/13/92 3:15:46 PM    gs    added dynamic check for sleep values.
  3. 13.2.1988 22:22:15 gs    }
  4.  
  5. unit TaskCtl;
  6. {© G. Sawitzki, StatLab Heidelberg 1986-1992}
  7.  
  8. {You only have to change this file if you want to add further menus,
  9. or if you want to implement a non Generic event handling.
  10.  
  11. You will rarely want to program a nongeneric response to an event. But if you
  12. want to, the recommended procedure is: Make a copy of the Generic event handler
  13. which you find in the unit "Generic". Paste the copy to your "TaskCtl". Rename
  14. it to something like My... throughout TaskCtl and adopt it to your needs. An
  15. example for this is given in "Random".
  16. Usually, you will just enhance the Generic Action, so My... will look like
  17.     begin
  18.         Appl...;        - the Generic Action -
  19.         xxxxxxx;        - your enhancement
  20.     end;
  21. }
  22.  
  23. interface
  24.  
  25.     uses
  26.            MacUnits, { Standard Includes}
  27.     StdTools, Generic,
  28.     NetWork,NetWorkLookUp,
  29.     ObjIntf,SchedulerUnit,NetSimGlobal, Task;
  30.  
  31. {tuning constants}
  32.     const
  33.         cForeGroundSleep = 0;{ticks to wait for wake up}
  34.         cBackgroundSleep=20;
  35.  
  36. {----------------------------command stuff----------------------}
  37. function mygrowzone (cbneeded: size): size;
  38. procedure MainEventLoop;
  39. procedure HandleFinderRequest;
  40.  
  41. implementation
  42. {-------------------- Generic Part -------------------------}
  43. procedure HandleFinderRequest;
  44.     begin
  45.     end;
  46.  
  47. {----------------Emergency exits this way-----------------}
  48.  
  49.  
  50. function mygrowzone;
  51.     begin
  52.         setfontlock(false);
  53.         mygrowzone := 0;
  54.     end;
  55.  
  56.  
  57. {-------------------- Application Part -------------------------}
  58.  
  59. {-------------------- Generic Part/ Menu Stuff-------------------------}
  60. procedure GenericFileMenu (theitem: integer);
  61.  
  62.     begin
  63.     {the last item should be quit}
  64.         if theitem = countMItems(GetMHandle(mFile)) then
  65.             gTaskState := TaskExit
  66.         else
  67.             case theitem of
  68.                 1: 
  69.                     doopen(cnew);        {New}
  70.                 2: 
  71.                     doopen(cOpen);        {Open}
  72.                 3: 
  73.                     doclose(cClose);    {Close}
  74.                 4: 
  75.                     dosave(cSave);        {Save}
  76.                 5: 
  77.                     dosave(cSaveAs);    {Save as...}
  78.                 6: 
  79.                     ;{(-}
  80.                 7: 
  81.                     ;{Print...}
  82.                 8: 
  83.                     ;{Page Setup...}
  84.             end;{case}
  85.     {Last entry should be Quit}
  86.     end;
  87.  
  88. {----------------------------command stuff-----------------------------------}
  89. procedure DoCommand (thecommand: Longint);
  90.     var
  91.         theMenu, theitem: integer;
  92.  
  93.  
  94.  
  95.     begin
  96.         theMenu := hiword(thecommand);
  97.         theitem := loword(thecommand);
  98.         case theMenu of
  99.             mApple: 
  100.                 GenericApple(theitem);         {will be handled generically}
  101.             mFile: 
  102.                 GenericFileMenu(theitem);    { The File menu. }
  103.             mEdit: 
  104.                 if not systemedit(theitem - 1) then
  105.                     GenericEdit(theitem);         { The Edit menu. }
  106.         { Check for accessory edit; if not, call the application edit. }
  107.             otherwise
  108.         end;{case}
  109.         hiliteMenu(0);              { Turn off the menu hiliting. }
  110.     end;
  111.  
  112. procedure DoMouseDown (event: EventRecord);
  113.     var
  114.         TempFrontWindow, WhichWindow: WindowPtr;    { Which window from findwindow. }
  115.         WhichWindowRef: longint;
  116.         fwreturncode: integer;        { findwindow return code. }
  117.         safeport: grafptr;            {Pointer to current port will be saved here during updates }
  118.         key: char;                    { Keyboard character. }
  119.         keycommand: Longint;        { Keybd input translated to menu equivs. }
  120.  
  121.     begin
  122.         with event do
  123.         begin
  124.  
  125.                     { Get the window/window part. This is a common information. So we extract it once.}
  126.             fwreturncode := findwindow(where, WhichWindow);
  127.  
  128.             case fwreturncode of
  129.  
  130.                 insyswindow: 
  131.                     systemclick(Event, WhichWindow);        { If in a system window, let the system Handle it. }
  132.  
  133.  
  134.                 inMenubar: 
  135.                     docommand(Menuselect(where));            { If in the menu bar, track it, then docommand }
  136.  
  137.                 indrag: 
  138.                     GenericDrag(Event, WhichWindow);
  139.  
  140.                 ingrow, incontent:{ If in the interior... }
  141.                 begin { If this window isn't in front, make it be so. }
  142.                     if (WhichWindow <> frontwindow) then
  143.                         selectwindow(WhichWindow)
  144.                             { Otherwise, let the application Handle it. }
  145.                     else
  146.                     begin
  147.                         getport(safeport);
  148.                         setport(WhichWindow);
  149.                         if fwreturncode = incontent then
  150.                             GenericMousedown(Event, WhichWindow)
  151.                         else
  152.                             GenericGrowwindow(Event, WhichWindow);
  153.                         setport(safeport);
  154.                     end;
  155.                 end;
  156.  
  157.                         { If in the go away box, track and call the appl}
  158.                 ingoaway: 
  159.                     if trackgoaway(WhichWindow, where) then
  160.                     begin
  161.                         GenericGoaway(WhichWindow);
  162.                         if WhichWindow <> nil then
  163.                         begin
  164.                             getport(safeport);
  165.                             DisposeWindow(WhichWindow);
  166.                             if safeport = WhichWindow then {avoid dangling grafport}
  167.                             begin
  168.                                 TempFrontWindow := frontwindow;
  169.                                 if TempFrontWindow = nil then
  170.                                 begin
  171.                                     GetWMgrPort(TempFrontWindow);{26.11.1990 0:53:50 Uhr    gs    }
  172.                                     setport(TempFrontWindow);
  173.                                 end
  174.                                 else
  175.                                     setwport(TempFrontWindow);
  176.                         end;{avoid dangling grafport}
  177.                         end;{whichwindow<>nil}
  178.                     end;{goAway}
  179.  
  180.                 InZoomIn, InZoomout: 
  181.                     if trackBox(WhichWindow, where, fwreturncode) then
  182.                     begin
  183.                         getport(safeport);
  184.                         setwport(whichwindow);
  185.                             GenericZoomWindow(fwreturncode,Event, WhichWindow);
  186.                         setport(safeport);
  187.                     end;{Zoom}
  188.                 otherwise
  189.             end;    {fwreturncode}
  190.         end;    {mousedown}
  191.     end;
  192.  
  193. {******************** The Event handler *****************}
  194. {DoEvent has been moved out of the main event loop to allow immediate testing}
  195.  
  196. procedure DoEvent (event: EventRecord);
  197.     var
  198.         TempFrontWindow, WhichWindow: WindowPtr;    { Which window from findwindow. }
  199.         WhichWindowRef: longint;
  200.         fwreturncode: integer;        { findwindow return code. }
  201.         safeport: grafptr;            {Pointer to current port will be saved here during updates }
  202.         key: char;                    { Keyboard character. }
  203.         keycommand: Longint;        { Keybd input translated to menu equivs. }
  204.  
  205.     begin
  206.         with Event do
  207.         begin
  208.             case what of
  209.                 mousedown: 
  210.                     DoMouseDown(event);
  211.  
  212.                 { Keyboard events: }
  213.                 keydown, autokey: 
  214.                 begin
  215.                     key := chr(bitand(Message, 255));
  216.                     if bitand(modifiers, cmdkey) <> 0 then
  217.                         keycommand := Menukey(key);
  218.                     if (what = keydown) and (bitand(modifiers, cmdkey) <> 0) and (hiword(keycommand) <> 0) then
  219.                         docommand(keycommand)
  220.                     else
  221.                         GenericKeydown(Event);
  222.                 end;
  223.  
  224.                 { Activate/deactivate window: }
  225.                 ActivateEvt: 
  226.                 begin
  227.                     getport(safeport);
  228.                     setport(WindowPtr(Message));
  229.                     if bitand(modifiers, 1) <> 0 then
  230.                         GenericActivate(WindowPtr(Message))
  231.                     else
  232.                         GenericDeactivate(WindowPtr(Message));
  233.                     setport(safeport);
  234.                 end;
  235.  
  236.  
  237.                 updateEvt:  { Update window: }
  238.                 begin
  239.                     getport(safeport);
  240.                     setport(WindowPtr(Message));
  241.                     beginupdate(WindowPtr(Message));
  242.                     if isDialogEvent(Event) then
  243.                         drawdialog(Dialogptr(Message))
  244.                     else
  245.                     begin
  246.                         drawcontrols(WindowPtr(Message));
  247.                         GenericUpdate(WindowPtr(Message));
  248.                     end;
  249.                     endupdate(WindowPtr(Message));
  250.                     setport(safeport);
  251.                 end;
  252.  
  253.                 diskEvt: 
  254.                     GenericDiskEvent(Message);{ Disk insertion event: }
  255.  
  256. {*******************}
  257.                 NetWorkEvt: begin
  258.                     NetWorkScheduler.HandleMsg(MsgPtr(message)); {required}
  259.                     NetWorkScheduler.periodictask; {optional: we want to give a prime priority to the scheduler}
  260.                     end;
  261. {*******************}
  262.  
  263.                 app4Evt: 
  264.                 begin
  265.                     TempFrontWindow := frontWindow;{26.11.1990 1:01:39 Uhr    gs    }
  266.                     if bitand(Message, $FF000000) = $01000000 then {suspend or resume}
  267.                     begin
  268.                         if odd(Message) then
  269.                         begin {will go to front}
  270.                             gWaitNextEventSleep:=cForegroundSleep;
  271.                             MyTask(BackEnd);
  272.                             gInBackground := false;
  273.                             if TempFrontWIndow <> nil then begin
  274.                                 selectwindow(TempFrontWindow); {should not be necessary, but is...}
  275.                                 invalrect(thePort^.PortRect);
  276.                             end;
  277.                         end
  278.                         else
  279.                         begin {will go to background}
  280.                             gWaitNextEventSleep:=cBackgroundSleep;
  281.                             MyTask(BackBegin);
  282.                             gInBackground := true;
  283.                             if TempFrontWIndow <> nil then
  284.                                 GenericDeactivate(TempFrontWindow);
  285.                         end;
  286.                     end;
  287.                 end;
  288.  
  289.                 nullEvent: 
  290.                     sysbeep(10); {should not come here}
  291.                 otherwise
  292.  
  293.             end;         {case}
  294.         end
  295.     end;
  296.  
  297.  
  298. {******************** The Event Loop *****************}
  299.  
  300. procedure MainEventLoop;
  301.     const
  302.         cmyEventLoopDelay=cEventLoopDelay; {3=1/20 second. recommended interval between WaitNextEvent
  303.                         for human interaction. we take 1 for machine responses}
  304.     var
  305.         newEvent: Eventrecord;        { Event from getnextEvent. }
  306.         hasWNE: boolean;
  307.         Eventreceived: boolean;
  308.     begin
  309.  
  310.         hasWNE := system.WNEIsImplemented;
  311.         gWaitNextEventSleep := 0;    {kick off}
  312.             gNextNetRefresh:=tickCount;
  313.  
  314.         {we are in the main event loop. we do not want to
  315.         spoil any tick. so we unroll our loop}
  316.         if hasWNE then
  317.         repeat                             { Loop until done. }
  318.             Eventreceived := WaitNextEvent(everyEvent, newEvent, gWaitNextEventSleep, nil);
  319.             {NIL: no mouse moved is wanted}
  320.             updatecursor;            { Change the cursor shape if appropriate. }
  321.  
  322.             if gInBackGround then gWaitNextEventSleep:=CBackgroundSleep {set proposal for next round}
  323.             else gWaitNextEventSleep:=CForeGroundSleep;
  324.  
  325.             SetEventLoopTime(cmyEventLoopDelay);
  326.             if Eventreceived then
  327.                 DoEvent(newEvent)
  328.             else {no real event, just timeout}
  329.  
  330.         {*******************************}
  331.         repeat
  332.         
  333.             NetWorkScheduler.periodictask;
  334.             if NLTask<>NoErr then ProgramBreak('NlTask Error');
  335.             gWaitNextEventSleep:=NetWorkScheduler.GetSleep;
  336.             MyTask(BackContinue);    {newEvent}
  337.             
  338.         until  (gTaskState<>TaskOk) |
  339.             {IsEventLoopTime:=} (LongIntPtr(Ticks)^>=gNextEventLoopTime);{IsEventLoopTime;}
  340.         {*******************************}
  341.  
  342.             if PAbortFlag then
  343.                 gTaskState := TaskCancel;
  344.             if gTaskState in [TaskExit, TaskFatal, TaskAbort] then
  345.                 gAppDone := true;
  346.         until gAppDone 
  347.         
  348.         else repeat                             { Loop until done. }
  349.  
  350.             systemtask;                { Let the system do its stuff. }
  351.             updatecursor;            { Change the cursor shape if appropriate. }
  352.             Eventreceived := getnextEvent(everyEvent, newEvent);
  353.  
  354.             if gInBackGround then gWaitNextEventSleep:=CBackgroundSleep {set proposal for next round}
  355.             else gWaitNextEventSleep:=CForeGroundSleep;
  356.  
  357.             SetEventLoopTime(cmyEventLoopDelay);
  358.             if Eventreceived then
  359.                 DoEvent(newEvent)
  360.             else {no real event, just timeout}
  361.  
  362.         {*******************************}
  363.         repeat
  364.         
  365.             NetWorkScheduler.periodictask;
  366.             if NLTask<>NoErr then ProgramBreak('NlTask Error');
  367.             gWaitNextEventSleep:=NetWorkScheduler.GetSleep;
  368.             MyTask(BackContinue);    {newEvent}
  369.             
  370.         until  (gTaskState<>TaskOk) |
  371.             {IsEventLoopTime:=} (LongIntPtr(Ticks)^>=gNextEventLoopTime);{IsEventLoopTime;}
  372.         {*******************************}
  373.  
  374.             if PAbortFlag then
  375.                 gTaskState := TaskCancel;
  376.             if gTaskState in [TaskExit, TaskFatal, TaskAbort] then
  377.                 gAppDone := true;
  378.         until gAppDone;
  379.         
  380.     end;{ End of main event loop. }
  381.  
  382. end.